package my12306;

public class Ticket {
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public Ticket() {
    }

    public Ticket(int num) {
        this.num = num;
    }

    public synchronized void sale() throws MyException {
        if (num - 1 == -1) {
            MyException e = new MyException(Thread.currentThread().getName() + "购票失败，余量不足！！！");
            throw e;
        }
        int before = this.num;
        this.setNum(before - 1);
        System.out.println(Thread.currentThread().getName() + "----->购票成功！" + "   票余量：" + this.getNum());

    }
}
